﻿import QtQuick 2.12
import QtQuick.Controls 1.4
import QtQuick.Controls 2.5
import QtQuick.Controls.Styles 1.4
import QtQml 2.12;
//我修改的

ApplicationWindow {
    visible: true
    width: 640
    height: 480

    TreeView {
        id: treeView
        anchors.fill: parent
        model: treeModel

        style: TreeViewStyle {
            branchDelegate: Item {
                width: 16
                height: 16
                Image {
                    anchors.fill: parent
                    source: styleData.isExpanded ?
                        "qrc:/icons/arrow-down.png" : "qrc:/icons/arrow-right.png"
                    visible: styleData.hasChildren
                }
            }

//            indentation: 0
        }

        headerVisible: false

        onModelChanged: {
            if (model) {
                model.expandAll()
            }
        }

        TableViewColumn {
            role: "display"
            title: "Name"
            width: 200
//            visible: false
        }


        rowDelegate:Rectangle{
            width: parent.width
            height: 40
        }

        itemDelegate: Item {
            height: 25
            Row {
                spacing: 5
                anchors.fill: parent

                CheckBox {
                    id: control
                    implicitWidth: 25
                    implicitHeight: 25
                    checkState: treeModel.data(styleData.index, Qt.CheckStateRole)

                    tristate:true
                    onCheckStateChanged: {
                        if (checkState === treeModel.data(styleData.index, Qt.CheckStateRole)) {
                            return
                        }

                        if(Qt.Unchecked === treeModel.data(styleData.index, Qt.CheckStateRole) && Qt.PartiallyChecked === checkState){
                            treeModel.setData(styleData.index, Qt.Checked, Qt.CheckStateRole)
                        } else  {
                            treeModel.setData(styleData.index, checkState, Qt.CheckStateRole)
                        }
                    }

                }
                Text {
                    text: styleData.value
                    verticalAlignment: Text.AlignVCenter
                }
            }
        }
    }

    Connections{
        target: treeModel
        onExpandTree:{
            console.log("hdj-------expand888")
             treeView.expand(index)
        }

    }

    // Test buttons
    Row {
        anchors.bottom: parent.bottom
        spacing: 10
        padding: 10

        TextField {
            id: filterField
            width: 100
            height: 30
            placeholderText: "输入节点名称进行过滤..."
            onTextChanged: treeModel.filter(text)
        }

        Button {
            text: "Get Checked Nodes"
            onClicked: {
                var checkedNodes = treeModel.getCheckedNodes();
                console.log("Checked Nodes:", checkedNodes);
            }
        }

        Button {
            text: "Check Node 4"
            onClicked: treeModel.setCheckedById([4,7], Qt.Checked)
        }

        Button {
            text: "Uncheck Node 4"
            onClicked: treeModel.setCheckedById([4,7], Qt.Unchecked)
        }
    }
}
